# from rest_framework import serializers
# from goods.models import SKU,SKUSpecification,GoodsCategory,SpecificationOption
#
# class SKUSpecificationSerializers(serializers.ModelSerializer):
#     spec_id = serializers.IntegerField(read_only=True)
#     option_id = serializers.IntegerField(read_only=True)
#     class Meta:
#         model = SKUSpecification
#         fields = [
#             'spec_id',
#             'option_id'
#         ]
#
# class SKUGoogsSerializers(serializers.ModelSerializer):
#     # specs = SKUSpecificationSerializers(read_only=True,many=True)
#     # category_id = serializers.IntegerField()
#     # category = serializers.StringRelatedField(read_only=True)
#     # spu_id = serializers.IntegerField()
#     # spu = serializers.StringRelatedField(read_only=True)
#     # class Meta:
#     #     model = SKU
#     #     field = '__all__'
#     spu = serializers.StringRelatedField()
#     spu_id = serializers.IntegerField()
#     category = serializers.StringRelatedField()
#     category_id = serializers.IntegerField()
#
#     # 代表的是，与当前sku主表对象关联的从表(SKUSpecification)数据集
#     specs = SKUSpecificationSerializers(many=True)
#
#     class Meta:
#         model = SKU
#         fields = "__all__"
#
# class GoodCategorySerializer(serializers.ModelSerializer):
#     class Meta:
#         model = GoodsCategory
#         fields = [
#             'id',
#             'name'
#         ]
#
# class SPUSimpleSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = GoodsCategory
#         fields = [
#             'id',
#             'name'
#         ]
#
# class SPUOptineSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = SpecificationOption
#         fields = [
#             'id',
#             'value'
#         ]
#
# class SPUSpecSerializer(serializers.ModelSerializer):
#     spu = serializers.StringRelatedField(read_only=True)
#     spu_id = serializers.IntegerField(read_only=True)
#     options = SPUOptineSerializer(read_only=True,many=True)
#     class Meta:
#         model = SpecificationOption
#         fields = "__all__"





# from rest_framework import serializers
# from goods.models import SKU,SKUSpecification,GoodsCategory,SPU,SPUSpecification,SpecificationOption
#
#
# class SpecOptionsSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = SpecificationOption
#         fields = [
#             'id',
#             'value'
#         ]
#
#
# class SPUSpecSerializer(serializers.ModelSerializer):
#     spu = serializers.StringRelatedField()
#     spu_id = serializers.IntegerField()
#
#     # 1、代表的数据是：与当前主表对象（规格）关联的所有从表对象（选项）
#     #                       颜色              [红色，黑色。。。。]
#     # 2、多个还是一个: 多个
#     # 3、需不需要自定义序列化器完成？答：需要
#     options = SpecOptionsSerializer(many=True)
#
#     class Meta:
#         model = SPUSpecification
#         fields = [
#             'id',
#             'name',
#             'spu',
#             'spu_id',
#             'options',
#         ]
#
#
#
#
#
# class SPUSimpleSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = SPU
#         fields = [
#             'id',
#             'name'
#         ]
#
#
# class GoodsCategoryDetailSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = GoodsCategory
#         fields = [
#             'id',
#             'name'
#         ]
#
#
#
# class SKUSpecModelSerializer(serializers.ModelSerializer):
#     spec_id = serializers.IntegerField()
#     option_id = serializers.IntegerField()
#
#     class Meta:
#         model = SKUSpecification
#         fields = [
#             'spec_id',
#             'option_id'
#         ]
#
#
# class SKUModelSerializer(serializers.ModelSerializer):
#     spu = serializers.StringRelatedField()
#     spu_id = serializers.IntegerField()
#     category = serializers.StringRelatedField()
#     category_id = serializers.IntegerField()
#
#     # 代表的是，与当前sku主表对象关联的从表(SKUSpecification)数据集
#     specs = SKUSpecModelSerializer(many=True)
#
#     class Meta:
#         model = SKU
#         fields = "__all__"
#
#
#     def create(self, validated_data):
#         # 添加业务逻辑：根据specs字段，新建中间表数据，来保存sku的规格和选项信息
#
#         # 1、有效数据中提取，中间表信息
#         specs = validated_data.pop('specs')
#         # 2、创建sku主表对象
#         sku_obj = super().create(validated_data)
#         # 3、创建中间表数据
#         # specs:  [{spec_id: "4", option_id: 8}, {spec_id: "5", option_id: 11}]
#         for spec in specs:
#             # spec: {spec_id: "4", option_id: 8}
#             spec['sku_id'] = sku_obj.id # {spec_id: "4", option_id: 8, "sku_id": sku_obj.id}
#             SKUSpecification.objects.create(**spec)
#
#         # 4、返回新建的sku数据对象
#         return sku_obj
#
#
#     def update(self, instance, validated_data):
#         # 修正中间表表数据
#
#         # 1、提取新的中间表信息
#         specs = validated_data.pop('specs')
#
#         # 更新本表数据
#         instance = super().update(instance, validated_data)
#
#         # 2、删除原来的中间表信息
#         SKUSpecification.objects.filter(sku_id=instance.id).delete()
#         # 3、新建中间表信息
#         for spec in specs:
#             # spec: {spec_id: "4", option_id: 8}
#             spec['sku_id'] = instance.id # {spec_id: "4", option_id: 8, "sku_id": sku_obj.id}
#             SKUSpecification.objects.create(**spec)
#
#         return instance



from rest_framework import serializers
from goods.models import SKU,SKUSpecification,GoodsCategory,SPU,SPUSpecification,SpecificationOption


# class SpecOptionsSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = SpecificationOption
#         fields = [
#             'id',
#             'value'
#         ]


# class SPUSpecSerializer(serializers.ModelSerializer):
#     spu = serializers.StringRelatedField()
#     spu_id = serializers.IntegerField()
#
#     # 1、代表的数据是：与当前主表对象（规格）关联的所有从表对象（选项）
#     #                       颜色              [红色，黑色。。。。]
#     # 2、多个还是一个: 多个
#     # 3、需不需要自定义序列化器完成？答：需要
#     options = SpecOptionsSerializer(many=True)
#
#     class Meta:
#         model = SPUSpecification
#         fields = [
#             'id',
#             'name',
#             'spu',
#             'spu_id',
#             'options',
#         ]





# class SPUSimpleSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = SPU
#         fields = [
#             'id',
#             'name'
#         ]
#
#
# class GoodsCategoryDetailSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = GoodsCategory
#         fields = [
#             'id',
#             'name'
#         ]
#
#
#
# class SKUSpecModelSerializer(serializers.ModelSerializer):
#     spec_id = serializers.IntegerField()
#     option_id = serializers.IntegerField()
#
#     class Meta:
#         model = SKUSpecification
#         fields = [
#             'spec_id',
#             'option_id'
#         ]
#
#
# class SKUModelSerializer(serializers.ModelSerializer):
#     spu = serializers.StringRelatedField()
#     spu_id = serializers.IntegerField()
#     category = serializers.StringRelatedField()
#     category_id = serializers.IntegerField()
#
#     # 代表的是，与当前sku主表对象关联的从表(SKUSpecification)数据集
#     specs = SKUSpecModelSerializer(many=True)
#
#     class Meta:
#         model = SKU
#         fields = "__all__"
#
#
#     def create(self, validated_data):
#         # 添加业务逻辑：根据specs字段，新建中间表数据，来保存sku的规格和选项信息
#
#         # 1、有效数据中提取，中间表信息
#         specs = validated_data.pop('specs')
#         # 2、创建sku主表对象
#         sku_obj = super().create(validated_data)
#         # 3、创建中间表数据
#         # specs:  [{spec_id: "4", option_id: 8}, {spec_id: "5", option_id: 11}]
#         for spec in specs:
#             # spec: {spec_id: "4", option_id: 8}
#             spec['sku_id'] = sku_obj.id # {spec_id: "4", option_id: 8, "sku_id": sku_obj.id}
#             SKUSpecification.objects.create(**spec)
#
#         # 4、返回新建的sku数据对象
#         return sku_obj
#
#
#     def update(self, instance, validated_data):
#         # 修正中间表表数据
#
#         # 1、提取新的中间表信息
#         specs = validated_data.pop('specs')
#
#         # 更新本表数据
#         instance = super().update(instance, validated_data)
#
#         # 2、删除原来的中间表信息
#         SKUSpecification.objects.filter(sku_id=instance.id).delete()
#         # 3、新建中间表信息
#         for spec in specs:
#             # spec: {spec_id: "4", option_id: 8}
#             spec['sku_id'] = instance.id # {spec_id: "4", option_id: 8, "sku_id": sku_obj.id}
#             SKUSpecification.objects.create(**spec)
#
#         return instance
#
#
#
#
#
#
#
#
# class SpecOptionsSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = SpecificationOption
#         fields = [
#             'id',
#             'value'
#         ]
#
#
# class SPUSpecSerializer(serializers.ModelSerializer):
#     spu = serializers.StringRelatedField()
#     spu_id = serializers.IntegerField()
#
#     # 1、代表的数据是：与当前主表对象（规格）关联的所有从表对象（选项）
#     #                       颜色              [红色，黑色。。。。]
#     # 2、多个还是一个: 多个
#     # 3、需不需要自定义序列化器完成？答：需要
#     options = SpecOptionsSerializer(many=True)
#
#     class Meta:
#         model = SPUSpecification
#         fields = [
#             'id',
#             'name',
#             'spu',
#             'spu_id',
#             'options',
#         ]
#
#




# option选项信息序列化
class SKU___SkuSpuSpecOptionSerializer(serializers.ModelSerializer):
    class Meta:
        model = SpecificationOption
        fields = [
            'id',
            'value'
        ]
# sku的spu规格信息
class SKU___SkuSpuSpecSimpleSerializer(serializers.ModelSerializer):
    spu = serializers.StringRelatedField()
    spu_id = serializers.IntegerField()
    options = SKU___SkuSpuSpecOptionSerializer(read_only=True,many=True)
    class Meta:
        model = SPUSpecification
        fields = [
            'id',
            'name',
            'spu',
            'spu_id',
            'options',
        ]

# SKU的spu表名称信息序列化器
# class SKU___SkuSpuSimpleSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = SPU
#         fields = ['id','name']
class SKU___SkuSpuSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SPU
        fields = [
            'id',
            'name'
        ]

# sku三级分类序列化器
class SKU___CategorySimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = ['id','name']

# sku规格选项序列化器
class SKU___SpecModelSerializer(serializers.ModelSerializer):
    spec_id = serializers.IntegerField()
    option_id = serializers.IntegerField()
    class Meta:
        model = SKUSpecification
        fields = ['spec_id','option_id']

# sku模型类i序列化器
class SKU___SkuModelSerializer(serializers.ModelSerializer):
    spu = serializers.StringRelatedField()
    category = serializers.StringRelatedField()
    spu_id = serializers.IntegerField()
    category_id = serializers.IntegerField()
    specs = SKU___SpecModelSerializer(read_only = True, many=True)
    class Meta:
        model = SKU
        fields = '__all__'